import { docLibraryAdd } from "@/services/knowledgeFile";
import {Form, Input, Modal, Radio, message, Table, Button, Menu, Icon, Pagination, Card} from "antd";
import React from "react";
import { formatMessage } from 'umi-plugin-react/locale';
import {ButtonGroup} from "@/utils/CommonComponent";
import { editDocTag, deleteDocTag,docTagListByPage,addDocTag } from "@/services/knowledgeFile";
import DocLibraryTagUpdate from "@/pages/KnowledgeFile/addDocLibraryTag";
import moment from "moment/moment";

class DocLibraryTag extends React.Component{
    constructor(props){
        super(props)
        this.state = {
            visible:false,
            libraryTarList:[],
            total:0,
            confirm:false,
        }
    }

    componentDidMount(){
        this.getList()
    }

    initOpen = () => {
        this.setState({
            visible:true
        },async () => {
            await this.getList()
        })
    }

    getList = async (cur)=>{
        let res = await docTagListByPage({current:cur? cur:1,size:10});
        this.setState({
            libraryTarList:res.records ? res.records : [],
            total:res.total,
            current:res.current,
        })
    }

    fenye = (page) => {
        this.getList(page).then(res=>{

        })
    }
    deleteTag = (row) => {
        Modal.confirm({
            title: formatMessage({id: 'knowledgeFile.docLibraryTag.deleteConfirm.title'}),
            okText: formatMessage({id: 'knowledgeFile.docLibraryTag.deleteConfirm.okText'}),
            cancelText: formatMessage({id: 'knowledgeFile.docLibraryTag.deleteConfirm.cancelText'}),
            okType:"danger",
            onOk:() => {
                deleteDocTag(row.id).then(res=>{
                    if(res.code === 0){
                        this.getList()
                        message.info(formatMessage({id: 'knowledgeFile.docLibraryTag.message.deleteSuccess'}))
                    }else {
                        message.error(res.message);
                    }
                })
            }
        })

    }

    onOk = () => {
        this.props.form.validateFields((error,values) => {
            if(!error){
                this.doOk(values)
            }
        })
    }

    doOk = async values => {
        const res = await docLibraryAdd({...values,type:0})
        if(res.code != -1){
            message.info(res.message)
            this.props.refreshList()
            this.props.form.resetFields()
        }
    }

    openUpdateModal = values => {
        const title = values ? 
            formatMessage({id: 'knowledgeFile.docLibraryTag.modal.editTitle'}) : 
            formatMessage({id: 'knowledgeFile.docLibraryTag.modal.addTitle'})
        this.DocLibraryTagUpdate.doInit(values,title)
    }
    render(){
        const { visible } = this.state;
        const { form:{getFieldDecorator} } = this.props;
        let columns = [
            {
                title: formatMessage({id: 'knowledgeFile.docLibraryTag.table.columns.tagName'}),
                align:"center",
                dataIndex: 'tagName',
            },
            {
                title: formatMessage({id: 'knowledgeFile.docLibraryTag.table.columns.weight'}),
                align:"center",
                dataIndex: 'weight',
            },
            {
                title: formatMessage({id: 'knowledgeFile.docLibraryTag.table.columns.createTime'}),
                dataIndex: 'createTime',
                align:"center",
                key: 'createTime',
                render: (createTime) => {
                    if (createTime) {
                        return moment(createTime).format('YYYY-MM-DD hh:mm:ss')
                    } else {
                        return createTime;
                    }
                }
            },
        ];
        columns.push({
            title: formatMessage({id: 'knowledgeFile.docLibraryTag.table.columns.operation'}),
            width:200,
            align:"center",
            fixed: 'right',
            render:(item,row) => {
                return <div>
                    <Button type="link" onClick={() => this.openUpdateModal(row.id)}>
                        {formatMessage({id: 'knowledgeFile.docLibraryTag.button.edit'})}
                    </Button>
                    <Button type="link" onClick={() => this.deleteTag(row)}>
                        <div style={{color:"red"}}>
                            {formatMessage({id: 'knowledgeFile.docLibraryTag.button.delete'})}
                        </div>
                    </Button>
                </div>
            }
        })
        return (
            <div>
                <Card bordered={false} style={{minHeight:"100%"}} bodyStyle={{paddingTop:10}}>
                    <div style={{display:"flex",justifyContent:'flex-end',marginBottom:10}}>
                        <div style={{flexShrink:'0'}}>
                            <ButtonGroup
                                title={formatMessage({id: 'knowledgeFile.docLibraryTag.button.add'})}
                                group="add"
                                handleClick={() => this.openUpdateModal()}
                            />
                        </div>
                    </div>
                    <Table
                        columns={columns}
                        rowKey="id"
                        pagination={false}
                        dataSource={this.state.libraryTarList}
                        scroll={{y: 'calc(100vh - 330px)' }}
                    />
                    {parseInt(this.state.total) > 10 ? (
                        <Pagination
                            style={{ textAlign: 'right', marginTop: 16 }}
                            onChange={(page) => this.fenye(page)}
                            current={this.state.current}
                            pageSize={10}
                            total={parseInt(this.state.total)}
                            showTotal={total => formatMessage(
                                {id: 'knowledgeFile.docLibraryTag.pagination.total'}, 
                                {total: total}
                            )}
                            showQuickJumper={true}
                        />
                    ) : null}
                    <DocLibraryTagUpdate
                        onRef={ref => this.DocLibraryTagUpdate = ref}
                        doRefresh={() => this.getList()}
                    />
                </Card>
            </div>

        )
    }
}

export default Form.create()(DocLibraryTag)